home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group01a.txt / 000055_icon-group-sender _Mon Jun 12 08:47:45 2000.msg < prev    next >
Internet Message Format  |  2002-01-03  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id IAA19788
  4.     for icon-group-addresses; Mon, 12 Jun 2000 08:47:29 -0700 (MST)
  5. Message-Id: <200006121547.IAA19788@baskerville.CS.Arizona.EDU>
  6. From: Steve Wampler <swampler@noao.edu>
  7. X-Newsgroups: comp.lang.icon
  8. Subject: Re: User defined operators for Icon
  9. Date: Tue, 06 Jun 2000 08:36:26 -0700
  10. X-Trace: noao.edu 960305789 18654 140.252.38.6 (6 Jun 2000 15:36:29 GMT)
  11. X-Complaints-To: abuse@noao.edu
  12. X-Accept-Language: en
  13. To: icon-group@optima.CS.Arizona.EDU
  14. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  15. Status: RO
  16. Content-Length: 2383
  17.  
  18. "Frank J. Lhota" wrote:
  19. > Languages such as C++ and Ada permit the programmer to define the behavior
  20. > of operators and predefined functions for user defined types. Would it be
  21. > desirable to add such a capacity to Icon? If so, what would this facility
  22. > look like?
  23.  
  24. Well, I have an opinion, though unicon is probably the place to pursue this,
  25. as I don't see any major enhancements being made to Icon right now.  Perhaps
  26. whatever works in unicon could be backed into Icon later.
  27.  
  28. There are several parts (others probably have better ways to do this):
  29.  
  30. (1) Have keywords for every function (as apposed to procedures) that
  31.     always get you the "original" definition.  This would be useful
  32.     now as it would allow access to the original functions even when
  33.     procedure definitions attach new meanings to the names.  There
  34.     may be a problem where existing keywords match existing function
  35.     names - that would need to be resolved.
  36.  
  37. (2) Have keywords for every operand (e.g. &+, &:=, etc.) which would
  38.     act in the same way that string invocation of operators does now,
  39.     but always reference the original definitions for the operators.
  40.  
  41. (3) add a way to assign new definitions to operators, perhaps with
  42.     syntax:
  43.  
  44.     operator "+"(a,b)
  45.        ...
  46.     end
  47.  
  48.     where the number of parameters determines which operation is
  49.     being redefined [could even generate compile-time error if
  50.     no such operation exists].
  51.  
  52.     So, adding complex numbers could be (well, you could do better
  53.     than this, but it's a start):
  54.  
  55.     operator +(a,b)
  56.  
  57.        if (type(a) == "complex") & (type(b) == "complex") then {
  58.            return complex(a.x + b.x, a.y + b.y)
  59.            # or: return complex( &+(a.x, b.x), &+(a.y,b.y))
  60.            }
  61.        return &+(a,b)
  62.  
  63.     end
  64.  
  65. Or course, unicon, with its inheritance, may be able to provide
  66. considerably more flexibility, since the above scheme only allows
  67. one level of redefinition...
  68.  
  69. Why do I like this?:
  70.  
  71. (a) most of the mechanisms for implementing this are already in place.
  72.  
  73. (b) there are minimal syntactic changes to the language (even less if
  74.     you go with using the reserved work "procedure" instead of adding
  75.     the reserved work "operator").
  76.  
  77. (c) with proper implementation, there should be little, if any,
  78.     performance penalty involved.
  79.  
  80. --
  81. Steve Wampler-  SOLIS Project, National Solar Observatory
  82. swampler@noao.edu
  83.